package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.TzjlCache;
import com.sandking.db.cache.YhCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 挑战记录
 */
public class Tzjl {

	public static final String TABLENAME = "挑战记录";
	public static final String CLASSNAME = "Tzjl"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** tempid */
	private int tempid;
	
	/** 挑战人id */
	private int tzrid;
	
	/** 目标人id */
	private int mbrid;
	
	/** 胜利 */
	private boolean sl;
	
	/** 开始时间 */
	private java.util.Date kssj;
	
	/** 结束时间 */
	private java.util.Date jssj;
	
	
	public Tzjl() {
		super();
	}
	
	public Tzjl(int id, int tempid, int tzrid, int mbrid, boolean sl, java.util.Date kssj, java.util.Date jssj) {
		super();
		this.id = id;
		this.tempid = tempid;
		this.tzrid = tzrid;
		this.mbrid = mbrid;
		this.sl = sl;
		this.kssj = kssj;
		this.jssj = jssj;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getTempid() {
		return tempid;
	}
	
	public void setTempid(int tempid) {
		this.tempid = tempid;
		addUpdateColumn("tempid",tempid);
	}
	
	public void changeTempidWith(int tempid){
		this.tempid += tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMin(int tempid,int min){
		this.tempid += tempid;
		this.tempid = this.tempid < min ? min : this.tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMax(int tempid,int max){
		this.tempid += tempid;
		this.tempid = this.tempid > max ? max : this.tempid;
		setTempid(this.tempid);
	}
	
	public void changeTempidWithMaxMin(int tempid,int max,int min){
		this.tempid += tempid;
		this.tempid = this.tempid < min ? min : this.tempid;
		this.tempid = this.tempid > max ? max : this.tempid;
		setTempid(this.tempid);
	}	
	public int getTzrid() {
		return tzrid;
	}
	
	public void setTzrid(int tzrid) {
		this.tzrid = tzrid;
		addUpdateColumn("挑战人id",tzrid);
	}
	
	public void changeTzridWith(int tzrid){
		this.tzrid += tzrid;
		setTzrid(this.tzrid);
	}
	
	public void changeTzridWithMin(int tzrid,int min){
		this.tzrid += tzrid;
		this.tzrid = this.tzrid < min ? min : this.tzrid;
		setTzrid(this.tzrid);
	}
	
	public void changeTzridWithMax(int tzrid,int max){
		this.tzrid += tzrid;
		this.tzrid = this.tzrid > max ? max : this.tzrid;
		setTzrid(this.tzrid);
	}
	
	public void changeTzridWithMaxMin(int tzrid,int max,int min){
		this.tzrid += tzrid;
		this.tzrid = this.tzrid < min ? min : this.tzrid;
		this.tzrid = this.tzrid > max ? max : this.tzrid;
		setTzrid(this.tzrid);
	}	
	public int getMbrid() {
		return mbrid;
	}
	
	public void setMbrid(int mbrid) {
		this.mbrid = mbrid;
		addUpdateColumn("目标人id",mbrid);
	}
	
	public void changeMbridWith(int mbrid){
		this.mbrid += mbrid;
		setMbrid(this.mbrid);
	}
	
	public void changeMbridWithMin(int mbrid,int min){
		this.mbrid += mbrid;
		this.mbrid = this.mbrid < min ? min : this.mbrid;
		setMbrid(this.mbrid);
	}
	
	public void changeMbridWithMax(int mbrid,int max){
		this.mbrid += mbrid;
		this.mbrid = this.mbrid > max ? max : this.mbrid;
		setMbrid(this.mbrid);
	}
	
	public void changeMbridWithMaxMin(int mbrid,int max,int min){
		this.mbrid += mbrid;
		this.mbrid = this.mbrid < min ? min : this.mbrid;
		this.mbrid = this.mbrid > max ? max : this.mbrid;
		setMbrid(this.mbrid);
	}	
	public boolean getSl() {
		return sl;
	}
	
	public void setSl(boolean sl) {
		this.sl = sl;
		addUpdateColumn("胜利",sl);
	}
	
	public java.util.Date getKssj() {
		return kssj;
	}
	
	public void setKssj(java.util.Date kssj) {
		this.kssj = kssj;
		addUpdateColumn("开始时间",kssj);
	}
	
	public java.util.Date getJssj() {
		return jssj;
	}
	
	public void setJssj(java.util.Date jssj) {
		this.jssj = jssj;
		addUpdateColumn("结束时间",jssj);
	}
	
	
	//挑战人id
	public Yh getYhPkTzrid(){
		return YhCache.getById(tzrid);
	}
	//目标人id
	public Yh getYhPkMbrid(){
		return YhCache.getById(mbrid);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("tempid", this.tempid);
        result.put("tzrid", this.tzrid);
        result.put("mbrid", this.mbrid);
        result.put("sl", this.sl);
        result.put("kssj", this.kssj);
        result.put("jssj", this.jssj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("tempid", this.tempid);
        result.put("挑战人id", this.tzrid);
        result.put("目标人id", this.mbrid);
        result.put("胜利", this.sl);
        result.put("开始时间", this.kssj);
        result.put("结束时间", this.jssj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.tempid);
		    SK_OutputStream.writeInt(out,this.tzrid);
		    SK_OutputStream.writeInt(out,this.mbrid);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Tzjl createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Tzjl tzjl = new Tzjl();
		    tzjl.id = SK_InputStream.readInt(in,null);
		    tzjl.tempid = SK_InputStream.readInt(in,null);
		    tzjl.tzrid = SK_InputStream.readInt(in,null);
		    tzjl.mbrid = SK_InputStream.readInt(in,null);
		    return tzjl;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Tzjl> createForColumnNameList(List<Map<String, Object>> list){
    	List<Tzjl> tzjls = new ArrayList<Tzjl>();
		for (Map<String, Object> map : list) {
			tzjls.add(createForColumnNameMap(map));
		}
		return tzjls;
    }
    
    /**
     * 根据map创建对象
     */
    public static Tzjl createForColumnNameMap(Map<String, Object> map){
    	Tzjl obj = new Tzjl();
	    obj.id = SK_Map.getInt("id", map);
	    obj.tempid = SK_Map.getInt("tempid", map);
	    obj.tzrid = SK_Map.getInt("挑战人id", map);
	    obj.mbrid = SK_Map.getInt("目标人id", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Tzjl> createForList(List<Map<String, Object>> list){
    	List<Tzjl> tzjls = new ArrayList<Tzjl>();
		for (Map<String, Object> map : list) {
			tzjls.add(createForColumnNameMap(map));
		}
		return tzjls;
    }
    
    /**
     * 根据map创建对象
     */
    public static Tzjl createForMap(Map<String, Object> map){
    	Tzjl obj = new Tzjl();
	    obj.id = SK_Map.getInt("id", map);
	    obj.tempid = SK_Map.getInt("tempid", map);
	    obj.tzrid = SK_Map.getInt("tzrid", map);
	    obj.mbrid = SK_Map.getInt("mbrid", map);
        return obj;
    }
    
    public static Tzjl createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Tzjl> createForJson(List<String> jsons){
    	List<Tzjl> tzjls = new ArrayList<Tzjl>();
    	for(String json : jsons){
    		tzjls.add(createForJson(json));
    	}
    	return tzjls;
    }
    
    /** 延迟插入数据库 */
    public Tzjl insert(){
    	return TzjlCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Tzjl update(){
    	return TzjlCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return TzjlCache.delete(this);
    }
    /** 即时插入数据库 */
    public Tzjl insertAndFlush(){
    	return TzjlCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Tzjl updateAndFlush(){
    	return TzjlCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return TzjlCache.deleteAndFlush(this);
    }
}